package com.example.p250201.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.p250201.entity.Account;
import com.example.p250201.mapper.LoginMapper;
import com.example.p250201.common.AppHttpCodeEnum;
import com.example.p250201.entity.RegisterSo;
import com.example.p250201.service.ILoginService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 袁可
 * @since 2025-02-03
 */
@Service
@Slf4j
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Account> implements ILoginService {

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public AppHttpCodeEnum insert(RegisterSo registerSo) {

        // 形参校验
        // 唯一约束，是否已存在，
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        if(!StringUtils.isBlank(registerSo.getPhone()) && !StringUtils.isBlank(registerSo.getEmail())){
            wrapper.eq("nickname",registerSo.getNickname())
                    .or()
                    .eq("phone",registerSo.getPhone())
                    .or()
                    .eq("email",registerSo.getEmail());
        }else if(registerSo.getPhone() != null) {
            wrapper.eq("nickname",registerSo.getNickname())
                    .or()
                    .eq("phone",registerSo.getPhone());
        }else if (!StringUtils.isBlank(registerSo.getEmail())) {
            wrapper.eq("nickname",registerSo.getNickname())
                    .or()
                    .eq("email",registerSo.getEmail());
        }else {
            log.error("注册失败,注册信息为空");
            return AppHttpCodeEnum.REGISTER_ERROR;
        }
        Long aLong =null;
        try {
            aLong = loginMapper.selectCount(wrapper);
        } catch (Exception e) {
            log.error("注册失败，selectCount方法异常",e);
            e.printStackTrace();
            return AppHttpCodeEnum.REGISTER_ERROR;
        }

        // 这个可以优化返回，具体哪个已存在，联合前端的输入校验，事件绑定，不归这个管，
        if (aLong>0){
            log.error("注册失败，注册信息已存在");
            return AppHttpCodeEnum.REGISTER_INFORM_EXIST;
        }

        // 属性拷贝
        Account account =new Account();
        BeanUtils.copyProperties(registerSo, account);

        // 数据补充
        account.setAccount(registerSo.getNickname());
        account.setSalt(registerSo.getNickname());
        // 生成密码
        account.setPassword(registerSo.getPassword()+account.getSalt());
        // 补充时间
        account.setCreateTime(LocalDateTime.now());
        account.setUpdateTime(LocalDateTime.now());

        // 账号注册
        Integer result = 0;
        try {
            result = loginMapper.insert(account);
        } catch (Exception e) {
            log.error("注册失败，insert异常",e);
            e.printStackTrace();
            return AppHttpCodeEnum.REGISTER_ERROR;
        }
        // 具体为什么注册失败，情况不同异常也不同，唯一键冲突，数据库错误，网络错误，
        log.error("注册成功");
        System.out.println("result = " + result);   // result=1
        System.out.println(account.getId()); //自增后的id会回填到对象中
        return AppHttpCodeEnum.SUCCESS;
    }
}
